from constant import test_data_path, answer_path, original_data_folder
import jsonlines
import json
import os
    

def read_data(file_path=test_data_path):
    test_list = []

    with open(file_path, 'r') as f:
        Q_data = jsonlines.Reader(f)

        for i in Q_data:
            test_list.append(i)
    
    return test_list


def write_data_jsonl(json_list, filename='Q_A_without_answer.jsonl'):
    with jsonlines.open(os.path.join(original_data_folder, filename.replace('.json', '_origin.json')), mode='w') as writer:
        for item in json_list:
            writer.write(item)

    json_list = [item for item in json_list if item != {}]
    print(os.path.join(original_data_folder, filename))
    with jsonlines.open(os.path.join(original_data_folder, filename), mode='w') as writer:
        for item in json_list:
            writer.write(item)

def write_data(answer_list, file_name='answer.json'):
    with open(os.path.join(answer_path, file_name), 'w')as f:
        json.dump(answer_list, f, ensure_ascii=False, indent=4)


def get_origin_answer(answer_list, original_list):
    index_ans = 0
    res_list = []
    for index in range(len(original_list)):
        if original_list[index] != {}:
            res_list.append(answer_list[index_ans])
            index_ans += 1
        else:
            res_list.append(["-1"])
    return res_list

